#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/10/17 16:49
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 队列 & 栈-设计循环队列.py
# @Software: PyCharm


class MyCircularQueue:

    def __init__(self, k):
        """
        Initialize your data structure here. Set the size of the queue to be k.
        :type k: int
        """
        self.len = k
        self.data = [0] * k
        self.head = -1
        self.rear = -1

    def enQueue(self, value):
        """
        Insert an element into the circular queue. Return true if the operation is successful.
        :type value: int
        :rtype: bool
        """
        if not self.len or self.len <= 0:
            return False
        if self.head == -1:
            self.head = 0
            self.rear = 1
            self.data[0] = value
            return True
        elif self.head == self.rear:
            return False
        else:
            self.data[self.rear] = value
            self.rear = (self.rear + 1) % self.len
            return True

    def deQueue(self):
        """
        Delete an element from the circular queue. Return true if the operation is successful.
        :rtype: bool
        """
        if self.head == -1:
            return False
        else:
            self.head = (self.head + 1) % self.len
            if self.head == self.rear:
                self.head = -1
            return True

    def Front(self):
        """
        Get the front item from the queue.
        :rtype: int
        """
        if self.head == -1:
            return -1
        else:
            return self.data[self.head]

    def Rear(self):
        """
        Get the last item from the queue.
        :rtype: int
        """
        if self.head == -1:
            return -1
        else:
            return self.data[(self.rear+self.len-1) % self.len]

    def isEmpty(self):
        """
        Checks whether the circular queue is empty or not.
        :rtype: bool
        """
        return self.head == -1

    def isFull(self):
        """
        Checks whether the circular queue is full or not.
        :rtype: bool
        """
        return self.head != -1 and self.head == self.rear

# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()


def main():
    obj = MyCircularQueue(3)
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.enQueue(1))
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.enQueue(2))
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.enQueue(3))
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.enQueue(4))
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.Rear())
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.isFull())
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.deQueue())
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.enQueue(4))
    print(obj.data)
    print(obj.head, obj.rear)
    print(obj.Rear())
    print(obj.data)
    print(obj.head, obj.rear)


if __name__ == '__main__':
    main()
